package com.bestcem.xm.ticket.service.manager.record;

import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.ticket.entity.mongo.TicketRecord;
import com.bestcem.xm.ticket.enums.RoleTimeLimitTypeEnum;
import com.bestcem.xm.ticket.service.manager.record.dto.AuditedCustomAttrDTO;
import com.bestcem.xm.ticket.service.manager.record.param.AuditedTicketRecordParam;
import lombok.Getter;
import lombok.ToString;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Optional;

/**
 * 工单进度记录 审核 信息工具类
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/18 16:11
 */
@ToString(callSuper = true)
@Getter
public final class AuditedTicketRecord extends BaseTicketRecord {
    public static final int MANUAL_CLOSED = 0;
    public static final int KEEP_FOLLOW = 1;

    /**
     * isAgreed
     */
    private Boolean isAgreed;
    /**
     * auditHandle
     */
    private Integer auditHandle;
    /**
     * timeLimit
     */
    private Integer timeLimit;
    /**
     * timeLimitType
     */
    private Integer timeLimitType;
    /**
     * timeLimitType
     */
    private LocalDateTime transferFollowDeadline;


    /**
     * 构建实例
     *
     * @param param 入参
     * @return 实例
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/8/20 16:01
     */
    public static AuditedTicketRecord getInstance(AuditedTicketRecordParam param) {
        if (Objects.isNull(param)) {
            return null;
        }
        return new AuditedTicketRecord(param);
    }

    private AuditedTicketRecord(AuditedTicketRecordParam param) {
        super(param);
        if (Objects.isNull(param)) {
            return;
        }
        this.isAgreed = param.getIsAgreed();
        this.auditHandle = param.getAuditHandle();
        this.timeLimit = 0;
        this.timeLimitType = 0;
        if (!this.isAgreed && this.auditHandle == KEEP_FOLLOW) {
            this.timeLimit = Optional.ofNullable(param.getTimeLimit()).orElse(0);
            this.timeLimitType = Optional.ofNullable(param.getTimeLimitType()).orElse(0);
            this.transferFollowDeadline = this.getInitDeadline();
        }
    }

    /***
     * 审核 记录的自定义模板字段信息
     * @return 模板字段信息
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/8/19 15:38
     */
    @Override
    public TicketRecord.CustomAttr customAttr() {
        AuditedCustomAttrDTO auditedCustomAttr = new AuditedCustomAttrDTO();
        auditedCustomAttr.setContent(this.getContent());
        auditedCustomAttr.setIsAgreed(this.isAgreed);
        auditedCustomAttr.setAuditHandle(this.auditHandle);
        auditedCustomAttr.setTimeLimit(this.timeLimit);
        auditedCustomAttr.setTimeLimitType(this.timeLimitType);
        LocalDateTime initDeadline = this.getInitDeadline();
        if (Objects.nonNull(initDeadline)) {
            auditedCustomAttr.setTransferFollowDeadline(DateUtil.getMongoDate(initDeadline));
        }
        return auditedCustomAttr.getCustomAttr();
    }

    private boolean hasLimit() {
        if (Objects.isNull(this.timeLimit) || Objects.isNull(this.timeLimitType)) {
            return false;
        }
        return (this.timeLimit != 0 && this.timeLimitType != 0);
    }

    private LocalDateTime getInitDeadline() {
        if (!hasLimit()) {
            return null;
        }
        LocalDateTime result = null;
        if (RoleTimeLimitTypeEnum.HOUR.getIndex() == this.timeLimitType) {
            result = LocalDateTime.now(DateUtil.BEIJING_ZONE).plusHours(this.timeLimit);
        }
        if (RoleTimeLimitTypeEnum.DAY.getIndex() == this.timeLimitType) {
            result = LocalDateTime.now(DateUtil.BEIJING_ZONE).plusDays(this.timeLimit);
        }
        return result;
    }
}
